#include <REGX52.H>
#include "AT24C02.h"
#include "key.h"
#include "LCD1602.h"
#include "delay.h"

unsigned char keyNumber;
unsigned int num;

void main()
{
	LCD_Init();
	LCD_ShowNum(1, 1, num, 5);
	
	while(1)
	{
		keyNumber = getKeyNumber();
		if(keyNumber == 1)
		{
			num++;
			LCD_ShowNum(1, 1, num, 5);
		}
		else if(keyNumber == 2)
		{
			num--;
			LCD_ShowNum(1, 1, num, 5);
		}
		else if(keyNumber == 3)
		{
			// 低8位放到0地址处
			AT24C02_writeByte(0, num%256);
			// 写入需要延迟5ms
			delay(5);
			// 高8位放到1地址处
			AT24C02_writeByte(1, num/256);
			// 写入需要延迟5ms
			delay(5);			
			LCD_ShowString(2, 1, "Write OK!");
			delay(1000);
			LCD_ShowString(2, 1, "         ");
		}
		else if(keyNumber == 4)
		{
			// 先取出低8位的值
			num = AT24C02_readByte(0);
			// 再取出高8位的值，左移8位和num相或，得到num
			num |= (AT24C02_readByte(1) << 8);
			LCD_ShowNum(1, 1, num, 5);
			LCD_ShowString(2, 1, "Read OK!");
			delay(1000);
			LCD_ShowString(2, 1, "        ");
		}
	}
}
